package com.example.demo.controller;

import com.example.demo.annotation.RequestAuthCheck;
import com.example.demo.config.ConstantConfig;
import com.example.demo.entity.BaseEntity;
import com.example.demo.entity.DictionaryCategory;
import com.example.demo.entity.dto.PageDto;
import com.example.demo.entity.vo.PageVo;
import com.example.demo.entity.vo.ResponseResult;
import com.example.demo.enums.StatusCodeEnum;
import com.example.demo.service.DictionaryCategoryService;
import com.example.demo.util.MyExcelUtil;
import com.example.demo.util.NormalUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;


/**
 * @description  字典类别相关接口控制器.
 * @author：lw
 * @create_time：2024/1/8
 **/


@Tag(name="字典类别接口")
@ApiSupport(order = 4)
@RestController
@RequestMapping(value = "/admin/dictionaryCategory")
public class DictionaryCategoryController {
    @Autowired
    DictionaryCategoryService dictionaryCategoryService;

    private final String menuName="dictionary";

    @Operation(summary = "新增")
    @ApiOperationSupport(order = 1,author = "lw")
    @RequestAuthCheck(menuName = menuName,action = ConstantConfig.add)
    @PostMapping(value = "/add")
    public ResponseResult<Boolean> add(@RequestBody List<DictionaryCategory> list) {
//        return  ResponseResult.build(dictionaryCategoryService.save(dictionaryCategory),StatusCodeEnum.add_success);
        return  ResponseResult.build(dictionaryCategoryService.saveBatch(list),StatusCodeEnum.add_success);
    }

    @Operation(summary = "删除")
    @ApiOperationSupport(order = 2,author = "lw")
    @RequestAuthCheck(menuName = menuName,action = ConstantConfig.delete)
    @PostMapping(value = "/deleteByIds")
    public ResponseResult<Boolean> deleteByIds(@RequestBody List<Long> ids) {
        //        UpdateWrapper<DictionaryCategory> updateWrapper=new UpdateWrapper<>();
//        updateWrapper.in("id",ids);
//        updateWrapper.set("is_deleted",1);
//        return  ResponseResult.build(dictionaryCategoryService.update(updateWrapper),StatusCodeEnum.delete_success);
        return  ResponseResult.build(dictionaryCategoryService.removeByIds(ids),StatusCodeEnum.delete_success);
    }

    @Operation(summary = "更新")
    @ApiOperationSupport(order = 3,author = "lw")
    @RequestAuthCheck(menuName = menuName,action = ConstantConfig.update)
    @PostMapping(value = "/update")
    public ResponseResult<Boolean> update(@RequestBody List<DictionaryCategory> list) {
//        return  ResponseResult.build(dictionaryCategoryService.updateById(dictionaryCategory),StatusCodeEnum.update_success);
        return  ResponseResult.build(dictionaryCategoryService.updateBatchById(list),StatusCodeEnum.update_success);
    }

    @Operation(summary = "获取详情")
    @ApiOperationSupport(order = 4 ,author = "lw")
    @GetMapping(value = "/detail")
    public ResponseResult<DictionaryCategory> getDetail(Long id) {
        return  ResponseResult.build(dictionaryCategoryService.getById(id),StatusCodeEnum.success);
    }

    @Operation(summary = "列表查询")
    @ApiOperationSupport(order = 5,author = "lw")
    @PostMapping(value = "/list")
    public ResponseResult<List<DictionaryCategory>> getList(@RequestBody DictionaryCategory dictionaryCategory) {
        return  ResponseResult.build(dictionaryCategoryService.getList(dictionaryCategory),StatusCodeEnum.query_success);
    }

    @Operation(summary = "分页查询")
    @ApiOperationSupport(order = 6,author = "lw")
    @PostMapping(value = "/page")
    public ResponseResult<PageVo<DictionaryCategory>> getPage(@RequestBody PageDto<DictionaryCategory> pageDto) {
        return  ResponseResult.build(dictionaryCategoryService.getPage(pageDto),StatusCodeEnum.query_success);
    }

    @Operation(summary = "导出")
    @ApiOperationSupport(order = 7,author = "lw")
    @RequestAuthCheck(menuName = menuName,action = ConstantConfig.export)
    @PostMapping(value = "/export")
    public void export(@RequestBody PageDto<DictionaryCategory> pageDto, HttpServletResponse response) {
        PageVo<DictionaryCategory> pageVo=dictionaryCategoryService.getPage(pageDto);
        MyExcelUtil.export(response,pageVo, NormalUtil.mergeMaps(DictionaryCategory.getExcelCols(), BaseEntity.getExcelCols()),DictionaryCategory.entityTitle);
    }

    @Operation(summary = "导入")
    @ApiOperationSupport(order = 8 ,author = "lw")
    @RequestAuthCheck(menuName = menuName,action = ConstantConfig.Import)
    @PostMapping(value = "/import")
    public ResponseResult<List<DictionaryCategory>>  importList(@RequestPart("file") MultipartFile file){
        List<DictionaryCategory> list=MyExcelUtil.read(file,DictionaryCategory.class);
        return ResponseResult.build(list,StatusCodeEnum.import_success);
    }
}

